﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FileType.cs" company="Anton Frattaroli">
//   Copyright (c) Anton Frattaroli. All rights reserved.
// </copyright>
// <summary>
//   Defines the FileType class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Lidocaine
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Globalization;

    /// <summary>
    /// Defines the FileType class.
    /// </summary>
    public class FileType
    {
        #region Fields
        /// <summary>
        /// The identity of the database table's row where this filetype object is saved.
        /// </summary>
        private int fileTypeID;

        /// <summary>
        /// Stores the extension map for this filetype.
        /// </summary>
        private string extension;

        /// <summary>
        /// Stores the filetype's MIME encoding.
        /// </summary>
        private string mime;
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the FileType class.
        /// </summary>
        public FileType()
        {
        }

        /// <summary>
        /// Initializes a new instance of the FileType class.
        /// </summary>
        /// <param name="fileTypeId">The identity of the database table's row referencing this filetype.</param>
        public FileType(int fileTypeId)
        {
            using (SqlConnection conn = new SqlConnection(Environmental.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.GetFileTypeByID", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@FileTypeID", fileTypeId);
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            rdr.Read();
                            this.fileTypeID = fileTypeId;
                            this.extension = rdr["Extension"].ToString();
                            this.mime = rdr["Mime"].ToString();
                        }
                        else
                        {
                            throw new DataException("Filetype does not exist.");
                        }
                    }
                }
            }
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the filetype's database row identity.
        /// </summary>
        public int FileTypeId
        {
            get
            {
                return this.fileTypeID;
            }
        }

        /// <summary>
        /// Gets or sets the filetype's file extension.
        /// </summary>
        public string Extension
        {
            get
            {
                return this.extension;
            }

            set
            {
                this.extension = value;
            }
        }

        /// <summary>
        /// Gets or sets the filetype's MIME encoding.
        /// </summary>
        public string Mime
        {
            get
            {
                return this.mime;
            }

            set
            {
                this.mime = value;
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Finds a filetype based on its associated file extension, if it exists.
        /// </summary>
        /// <param name="extension">The extension to search for (e.g. "htm")</param>
        /// <returns>If the extension is found, an HFileType is populated and returned. Returns null otherwise.</returns>
        public static FileType GetFileTypeByExtension(string extension)
        {
            FileType h = null;
            using (SqlConnection conn = new SqlConnection(Environmental.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.GetFileTypeByExtension", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Extension", extension);
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            rdr.Read();
                            h = new FileType();
                            h.fileTypeID = Convert.ToInt32(rdr["FileTypeID"], CultureInfo.InvariantCulture);
                            h.extension = extension;
                            h.mime = rdr["Mime"].ToString();
                        }
                    }
                }
            }

            return h;
        }

        /// <summary>
        /// Gets a list of the filetypes associated with this application.
        /// </summary>
        /// <returns>A read only list of HFileType type object.</returns>
        public static IList<FileType> GetFileTypes()
        {
            List<FileType> filetypes = new List<FileType>();
            using (SqlConnection conn = new SqlConnection(Environmental.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.GetFileTypes", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            while (rdr.Read())
                            {
                                filetypes.Add(new FileType(Convert.ToInt32(rdr["FileTypeID"], CultureInfo.InvariantCulture)));
                            }
                        }
                    }
                }
            }

            return filetypes;
        }

        /// <summary>
        /// Inserts the HFileType object into the database.
        /// </summary>
        public void SaveAs()
        {
            using (SqlConnection conn = new SqlConnection(Environmental.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.InsertFileType", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Extension", this.extension);
                    cmd.Parameters.AddWithValue("@Mime", this.mime);
                    this.fileTypeID = Convert.ToInt32(cmd.ExecuteScalar(), CultureInfo.InvariantCulture);
                }
            }
        }

        /// <summary>
        /// Updates the HFileType's database fields.
        /// </summary>
        public void Save()
        {
            using (SqlConnection conn = new SqlConnection(Environmental.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.UpdateFileType", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Extension", this.extension);
                    cmd.Parameters.AddWithValue("@Mime", this.mime);
                    cmd.ExecuteNonQuery();
                }
            }
        }

        /// <summary>
        /// Deletes the HFileType from the database.
        /// </summary>
        public void Delete()
        {
            using (SqlConnection conn = new SqlConnection(Environmental.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.DeleteFileType", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@FileTypeID", this.fileTypeID);
                    cmd.ExecuteNonQuery();
                }
            }
        }
        #endregion
    }
}